package com.cnbot.aimolib.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cnbot.aimolib.utils.SerializeKit;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author ytf
 * Created by on 2021/06/01 15:41
 */
public class AimoFaceDbManager {

    private static AimoFaceDbManager instance;

    public static AimoFaceDbManager getInstance(){
        if(instance == null){
            synchronized(AimoFaceDbManager.class){
                if(instance == null){
                    instance = new AimoFaceDbManager();
                }
            }
        }
        return instance;
    }

    private volatile AtomicBoolean isInited = new AtomicBoolean(false);
    private AiMoFaceDbHelper dbHelper;
    private SQLiteDatabase database;

    public void init(Context context){
        if(!isInited.getAndSet(true)){
            dbHelper = new AiMoFaceDbHelper(context);
            database = dbHelper.getWritableDatabase();
        }
    }

   private AimoFaceDbData getBeanFromCursor(Cursor cursor){
       int idx = cursor.getColumnIndex(AiMoFaceDbHelper.FIELD_ID);
       int fdx = cursor.getColumnIndex(AiMoFaceDbHelper.FIELD_FEATURE);
       int pdx = cursor.getColumnIndex(AiMoFaceDbHelper.FIELD_PATH);
       int udx = cursor.getColumnIndex(AiMoFaceDbHelper.FIELD_USERID);
       AimoFaceDbData bean = new AimoFaceDbData();
       if(idx != -1){
           bean._id = cursor.getInt(idx);
       }
       if (fdx != -1){
           byte[] bytes = cursor.getBlob(fdx);
           if(bytes != null){
               bean.feature = SerializeKit.byteToObject(bytes);
           }
       }
       if(pdx != -1){
           bean.path = cursor.getString(pdx);
       }
       if(udx != -1){
           bean.user_id = cursor.getString(udx);
       }
       return bean;
   }

    public synchronized AimoFaceDbData querFaceByUserId(String userId) {
        Cursor cursor = database.rawQuery("select * from " + AiMoFaceDbHelper.TABLE_NAME + " where " + AiMoFaceDbHelper.FIELD_USERID + "=?", new String[]{userId});

        if(cursor.moveToFirst()){
            AimoFaceDbData record = getBeanFromCursor(cursor);
            cursor.close();
            return record;
        }
        cursor.close();
        return null;
    }

    /**
     * 插入数据
     */
    public synchronized long insertOrReplace(AimoFaceDbData bean) {

        //生成要修改或者插入的键值
        ContentValues cv = new ContentValues();
        cv.put(AiMoFaceDbHelper.FIELD_ID, bean._id);
        cv.put(AiMoFaceDbHelper.FIELD_PATH, bean.path);
        byte[] bytes = SerializeKit.objectToByte(bean.feature);
        cv.put(AiMoFaceDbHelper.FIELD_FEATURE, bytes);
        cv.put(AiMoFaceDbHelper.FIELD_USERID, bean.user_id);
        return database.replace(AiMoFaceDbHelper.TABLE_NAME, null, cv);
    }

    /**
     * 查询全部数据
     */
    public synchronized List<AimoFaceDbData> listAll() {

        List<AimoFaceDbData> list = new ArrayList<>();
        Cursor cursor = database.rawQuery("select * from " + AiMoFaceDbHelper.TABLE_NAME, new String[0]);

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                list.add(getBeanFromCursor(cursor));
            }
        }
        cursor.close();
        return list;
    }

    public synchronized void deleteByUserId(String userId){
        database.delete(AiMoFaceDbHelper.TABLE_NAME, AiMoFaceDbHelper.FIELD_USERID + "=?", new String[]{userId});
    }

    public synchronized void delete(AimoFaceDbData data){
        database.delete(AiMoFaceDbHelper.TABLE_NAME, AiMoFaceDbHelper.FIELD_ID + "=?", new String[]{String.valueOf(data._id)});
    }
}
